﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Runtime.Serialization;
using System.IO;
using Northwind.NET.Model;
using System.Diagnostics;
using System.Data.Common;
using Helper = NorthwindTests.NorthwindTestHelper;
namespace NorthwindTests
{	
	[TestClass]
	public class UnitTest1
	{		

		#region Additional test attributes
		//
		// You can use the following additional attributes as you write your tests:
		//
		// Use ClassInitialize to run code before running the first test in the class
		// [ClassInitialize()]
		// public static void MyClassInitialize(TestContext testContext) { }
		//
		// Use ClassCleanup to run code after all tests in a class have run
		// [ClassCleanup()]
		// public static void MyClassCleanup() { }
		//
		// Use TestInitialize to run code before running each test 
		// [TestInitialize()]
		// public void MyTestInitialize() { }
		//
		// Use TestCleanup to run code after each test has run
		// [TestCleanup()]
		// public void MyTestCleanup() { }
		//
		#endregion

		[TestMethod]
		public void StackOverflowExceptionOnSerialize()
		{
			SequencedObject seqobj = new Customer { Address = "K. G. Harris", City = "Madison", PostalCode = "53217" };
			Customer c = (Customer)seqobj;

			//Stackoverflow exception:
			Assert.AreEqual(false, Helper.CanSerializeDeserialize<Customer>(c));
			Assert.AreEqual(false, Helper.CanSerializeDeserialize<SequencedObject>(seqobj));
		}


		[TestMethod]
		public void TestMethod2()
		{
			Shipper s = new Shipper
			{
				ID = 2391,
				Name = "Allstar Health",
				Orders = new OrderCollection { 
					new Order { ShipPostalCode = "20151", CustomerId = 991, Freight = (Decimal)42.0, Customer = new Customer { PostalCode = "20151"} } 
				}
			};
			SequencedObject so = s;
			Assert.AreEqual(false, Helper.CanSerializeDeserialize<SequencedObject>(so));
			Assert.AreEqual(false, Helper.CanSerializeDeserialize<Shipper>(s));
		}

		[TestMethod]
		public void TestDAL_Execute()
		{
			using (Helper.dbconnection)
			{
				Helper.dbconnection.Open();
				var processor = new Northwind.NET.DAL.DbQueryProcessor(Helper.dbconnection);				
				var s = processor.Execute<Supplier>().ToList();
				Assert.IsTrue(s.Count() > 0);
				var c = processor.Execute<Customer>().ToList(); 
				Assert.IsTrue(c.Count() > 0);
				var p = processor.Execute<Product>().ToList(); 
				Assert.IsTrue(p.Count() > 0);
				var shippers = processor.Execute<Shipper>().ToList(); 
				Assert.IsTrue(shippers.Count() > 0);
			}			
		}


	
	
	}
}
